<?php

namespace iflow\template\document\Tag;

use iflow\template\config\Config;
use iflow\template\document\abstracts\TagAbstract;
use iflow\template\document\Parser\DOMNodeParser;
use iflow\template\document\Parser\ParserHtml;
use iflow\template\document\Tag\Exceptions\TagAttributesNullException;

class ClassTag extends TagAbstract {


    protected array $hiddenAttributes = [
        'tag', 'iclass', 'paramsname', 'props', 'method', 'methodprops'
    ];


    protected ?string $i_class = '';
    protected ?string $paramsName = '';
    protected ?string $method = '';
    protected ?string $methodProps = '';


    public function parser(DOMNodeParser $node, Config $config, ParserHtml $parserHtml): static {
        parent::parser($node, $config, $parserHtml); // TODO: Change the autogenerated stub

        $this->i_class = $this->node -> getAttributes('iclass');
        $this->method = $this->node -> getAttributes('method');
        $this->methodProps = $this->node -> getAttributes('methodProps');
        $this->paramsName = $this->node -> getAttributes('paramsName');

        return $this->parserAttributes();
    }


    public function parserAttributes(): static {

        if (!$this->i_class) throw new TagAttributesNullException('className does not exist');
        $props = $this -> props ? "[{$this -> props}]" : "";
        $methodProps = $this -> methodProps ? "...[{$this -> methodProps}]" : "";

        $i_html = $this->paramsName ? "\${$this->paramsName} = app('{$this -> i_class}', {$props});" : "app('{$this -> i_class}', {$props});";

        if ($this->method) {
            $i_html .= "\${$this->paramsName} = {$this->paramsName} -> {$this -> method}($methodProps);";
        }

        $this->html = sprintf('<?php %s ?>', $i_html);
        return $this;
    }

}